import pygame
from sys import exit
from pygame.locals import *
import os
import random

os.environ["SDL_VIDEO_CENTERED"] = "1"

pygame.init()
tela = pygame.display.set_mode((640, 480), 0, 32)

pygame.init()
pygame.display.set_caption("FittingBricks")
pygame.mouse.set_visible(False)

#funcao que faz os marcadores surgirem aleatoriamente.
def bloco_aleatorio():
    aleatorio = random.randint(1,7)
    blocos = { 1:"bloco1.png", 2:"bloco2.png", 3:"bloco3.png",4:"bloco4.png", 5:"bloco5.png", 6:"bloco6.png",7:"bloco7.png" }
    bloco_da_vez = blocos.get(aleatorio)
    return bloco_da_vez


#imagens
fundo1 = pygame.image.load("./Imagens/tela1.1.jpg")
fundo2 = pygame.image.load("./Imagens/tela2.jpg")
fundo3 = pygame.image.load("./Imagens/tela3.jpg")
fundo4 = pygame.image.load("./Imagens/tela4.jpg")
peca = pygame.image.load("Imagens" + os.sep + bloco_aleatorio())

#musica do jogo
pygame.mixer.init
pygame.mixer.music.load('./sons/Tetris1.mp3')
pygame.mixer.music.play(-1)
toca = True


#Funcao do menu sobre
def menu_sobre():
    sobre_on = True
    #tela.blit(fundo4,(0,0))
    pygame.display.update()
    while sobre_on == True:
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    sobre_on = False
                elif event.type == KEYDOWN:
                    if event.key == K_m:
                        if toca:
                            pygame.mixer.music.pause()
                            toca = False
                        else:
                            pygame.mixer.music.unpause()
                            toca = True
                elif event.key == K_RETURN:
                    pygame.display.set_mode((800, 480), 0 , 32)
        tela.blit(fundo4, (0,0))
        pygame.display.update()

#Funcao do menu das instrucoes
def menu_instrucao():
    instr_on = True
    tela.blit(fundo2, (0,0))
    pygame.display.update()
    while instr_on == True:
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    instr_on = False
                elif event.type == KEYDOWN:
                    if event.key == K_m:
                        if toca:
                            pygame.mixer.music.pause()
                            toca = False
                        else:
                            pygame.mixer.music.unpause()
                            toca = True                    
                elif event.key == K_RETURN:
                    pygame.display.set_mode((800, 480), 0 , 32)
        tela.blit(fundo2, (0,0))
        pygame.display.update()


#Funcao do menu de creditos
def menu_creditos():
    creditos_on = True
    tela.blit(fundo3, (0,0))
    pygame.display.update()
    while creditos_on == True:
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    creditos_on = False
                elif event.type == KEYDOWN:
                    if event.key == K_m:
                        if toca:
                            pygame.mixer.music.pause()
                            toca = False
                        else:
                            pygame.mixer.music.unpause()
                            toca = True
                elif event.key == K_RETURN:
                    pygame.display.set_mode((800, 480), 0 , 32)

        tela.blit(fundo3, (0,0))
        pygame.display.update()


#Funcao do menu principal\inicial   
def menu_principal():
    global toca
    opcao = 1
    opcoes = {1:(143, 120), 2:(143, 188), 3:(143, 256),4:(143, 327),5:(143, 395)}
    menu_aberto = True
    tela.blit(fundo1, (0,0))
    peca = pygame.image.load("Imagens" + os.sep + bloco_aleatorio())
    tela.blit(peca, (opcoes[opcao]))
    pygame.display.update()
    while menu_aberto == True:
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()
            elif event.type == KEYDOWN:
                if event.key == K_m:
                    if toca:
                        pygame.mixer.music.pause()
                        toca = False
                    else:
                        pygame.mixer.music.unpause()
                        toca = True     
        
                if event.key == K_DOWN:
                    peca = pygame.image.load("Imagens" + os.sep + bloco_aleatorio())
                    opcao += 1
                    if opcao > 5:
                        opcao = 1
                elif event.key == K_UP:
                    opcao -= 1
                    if opcao < 1:
                        opcao = 4
                elif event.key == K_RETURN:
                    if opcao == 1:
                        import tetrix
                        tetrix.main()   
                        menu_aberto = False
                        
                    elif opcao == 2:
                        menu_instrucao()
                        
                    elif opcao == 3:
                        menu_creditos()

                    elif opcao == 4:
                         menu_sobre()

                    elif opcao == 5:
                         exit()
                elif event.key == K_ESCAPE:
                    exit()

        if menu_aberto == True:
            pygame.display.update()
            tela.blit(fundo1, (0,0))
            tela.blit(peca, (opcoes[opcao]))

menu_principal()
